﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Image2d
{
    class Object2D
    {
        private Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        private Texture2D image;
        public Texture2D Image
        {
            get { return image; }
            set { image = value; }
        }
        private Color cor;
        public Color Cor
        {
            get { return cor; }
            set { cor = value; }
        }
        private Rectangle rect;
        public Rectangle Rect
        {
            get { return rect; }
            set { rect = value; }
        }
        private float rotation;
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }
        private Vector2 origin;
        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }
        private Vector2 scale;
        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        private SpriteEffects effects;
        public SpriteEffects Effects
        {
            get { return effects; }
            set { effects = value; }
        }
        private float layerDepth;
        public float LayerDepth
        {
            get { return layerDepth; }
            set { layerDepth = value; }
        }

        public Object2D(Texture2D image, Vector2 position)
        {
            this.image = image;
            this.position = position;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(image, position, cor);
        }
        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(image, position, rect,cor, rotation, origin, scale, effects, layerDepth);
        }
    }

    class AnimatedObject2D
    {
        private Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        private Texture2D image;
        public Texture2D Image
        {
            get { return image; }
            set { image = value; }
        }
        private Color cor;
        public Color Cor
        {
            get { return cor; }
            set { cor = value; }
        }
        private Rectangle rect;
        public Rectangle Rect
        {
            get { return rect; }
            set { rect = value; }
        }
        private float rotation;
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }
        private Vector2 origin;
        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }
        private Vector2 scale;
        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        private SpriteEffects effects;
        public SpriteEffects Effects
        {
            get { return effects; }
            set { effects = value; }
        }


        //nivel de caman do objeto
        private float layerDepth;
        //acesso ao layerDepth
        public float LayerDepth
        {
            get { return layerDepth; }
            set { layerDepth = value; }
        }



        private Animation[] animations;
        public Animation[] Animations
        {
            get { return animations; }
        }
        public Animation currentAnimation;

        public AnimatedObject2D(Texture2D image, Vector2 position, Animation[] animations)
        {
            this.animations = animations;
            currentAnimation = animations[0];
            this.image = image;
            this.position = position;
            this.rect = new Rectangle((int)currentAnimation.startPosition.X, 
                (int)currentAnimation.startPosition.Y,
                (int)currentAnimation.frameSize.X, 
                (int)currentAnimation.frameSize.Y);
            this.cor = Color.White;
            this.rotation = 0f;
            this.scale = Vector2.One;
            this.effects = SpriteEffects.None;
            this.origin = Vector2.Zero;
            this.layerDepth = 0f;
        }

        public virtual void Update(GameTime gameTime)
        {
            rect = currentAnimation.UpdateRect(rect, gameTime);
        }
        public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {            
            spriteBatch.Draw(image, position, rect,cor, rotation, origin, scale, effects, layerDepth);
        }
    }

    struct Animation
    {
        public Vector2 startPosition;
        public Vector2 currentFrame;
        public Vector2 totalFrames;
        public Vector2 frameSize;

        private float frameR;
        public float FrameRate
        {
            set { frameR = value; }
            get { return 1000 / frameR; }
        }
        public float updateTime;

        public string Name;

        public Rectangle UpdateRect(Rectangle rect, GameTime gameTime)
        {
            if (updateTime <= (float)gameTime.TotalGameTime.TotalMilliseconds)
            {
                if(updateTime != 0)currentFrame.X++; 
                updateTime += FrameRate; 

                if (currentFrame.X >= totalFrames.X) 
                {
                    currentFrame.X = 0;
                    currentFrame.Y++;
                    if (currentFrame.Y >= totalFrames.Y)
                    {
                        currentFrame.Y = 0;
                    }
                }

                rect = new Rectangle((int)(startPosition.X + currentFrame.X * frameSize.X),
                    (int)(startPosition.Y + currentFrame.Y * frameSize.Y),
                    (int)frameSize.X, (int)frameSize.Y);
            }

            return rect;
        }
    }
}
